/*
 * Copyright (c) 2005 -2008 Reliable Source, Inc. and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Mozilla Public License v.1.1,
 * BSD, and Eclipse Public License v1.0 which accompanies this
 * distribution and is available at:
 *
 * 		http://www.eclipse.org/legal/epl-v10.html
 * 		http://www.opensource.org/licenses/bsd-license.php
 * 		http://www.mozilla.org/MPL/MPL-1.1.html
 *
 * Contributors:
 *     IBM Corporation	     -  http://ibm.com
 *     	- initial API and implementation
 *
 *     patientos.org         - http://patientos.org
 *     	- domain and database persistence layer
 *
 *     Reliable Source, Inc. - http://reliablesource.org
 *     	- Symbiosys Application Framework,
 *     	- Symbiosys Application, Symbiosys Product
 *
 */
package org.reliablesource.symbiosys.workbench.tests.adapters;

import java.util.HashMap;

import org.eclipse.core.runtime.IAdaptable;
import org.eclipse.core.runtime.IAdapterFactory;
import org.eclipse.ui.IEditorInput;
import org.eclipse.ui.model.IWorkbenchAdapter;
import org.eclipse.ui.model.IWorkbenchAdapter2;
import org.eclipse.ui.progress.IDeferredWorkbenchAdapter;
import org.eclipse.ui.views.properties.IPropertySource;
import org.reliablesource.model.core.IReliableElement;


/**
 *
 * MockObjectAdapterFactory.java<br>
 * Created on: Jun 27, 2008<br>
 *
 * @author <a href="mailto:dennis.park@gmail.com">Dennis Park</a>
 *
 * @category
 * @since 1.0
 */
public class MockObjectAdapterFactory implements IAdapterFactory {
    private static MockObjectAdapterFactory instance;
    private static final Class<?>[] adapterList = {
            IPropertySource.class, IAdaptable.class, IEditorInput.class,
            IWorkbenchAdapter.class, IWorkbenchAdapter2.class,
            IDeferredWorkbenchAdapter.class
        };
    private static HashMap<Integer, MockObjPropertySourceAdapter> propAdapterMap;
    private static HashMap<Integer, MockWorkbenchAdapter> workbenchAdapterMap;
    private static HashMap<Integer, MockSlowElementAdapter> slowAdapterMap;

    static {
        instance = new MockObjectAdapterFactory();
        propAdapterMap = new HashMap<Integer, MockObjPropertySourceAdapter>();
        workbenchAdapterMap = new HashMap<Integer, MockWorkbenchAdapter>();
        slowAdapterMap = new HashMap<Integer, MockSlowElementAdapter>();
    }

    private MockObjPropertySourceAdapter cmpPrpSrcAdapter;
    private MockSlowElementAdapter cmpSlowElementAdapter;
    private MockWorkbenchAdapter cmpWorkbenchAdapter;

    /**
     * Creates a new CMPMockObjectAdapterFactory object.
     */
    private MockObjectAdapterFactory() {
        //
    }

    /**
     * @return MockObjectAdapterFactory - the singleton instance.
     */
    public static synchronized MockObjectAdapterFactory getInstance() {
        return instance;
    }

    /*
     * (non-Javadoc)
     *
     * @see org.eclipse.core.runtime.IAdapterFactory#getAdapter(java.lang.Object,
     *      java.lang.Class)
     */
    @SuppressWarnings("unchecked")
    public Object getAdapter(Object adaptableObject, Class adapterType) {
        Object returnAdapter = null;

        if (adaptableObject instanceof IReliableElement) {
            if (adapterType.equals(IPropertySource.class)) {
                if (propAdapterMap.containsKey(adaptableObject.hashCode())) {
                    returnAdapter = propAdapterMap.get(adaptableObject.hashCode());
                } else {
                    cmpPrpSrcAdapter = new MockObjPropertySourceAdapter((IReliableElement) adaptableObject);
                    propAdapterMap.put(adaptableObject.hashCode(),
                        cmpPrpSrcAdapter);
                    returnAdapter = cmpPrpSrcAdapter;
                }
            } else if (adapterType.equals(IWorkbenchAdapter.class) ||
                    adapterType.equals(IWorkbenchAdapter2.class)) {
                if (workbenchAdapterMap.containsKey(adaptableObject.hashCode())) {
                    returnAdapter = workbenchAdapterMap.get(adaptableObject.hashCode());
                } else {
                    cmpWorkbenchAdapter = new MockWorkbenchAdapter();
                    cmpWorkbenchAdapter.setObjectToAdapt((IReliableElement) adaptableObject);
                    workbenchAdapterMap.put(adaptableObject.hashCode(),
                        cmpWorkbenchAdapter);
                    returnAdapter = cmpWorkbenchAdapter;
                }
            } else if (adapterType.equals(IDeferredWorkbenchAdapter.class)) {
                if (slowAdapterMap.containsKey(adaptableObject.hashCode())) {
                    returnAdapter = slowAdapterMap.get(adaptableObject.hashCode());
                } else {
                    cmpSlowElementAdapter = new MockSlowElementAdapter((IReliableElement) adaptableObject);
                    slowAdapterMap.put(adaptableObject.hashCode(),
                        cmpSlowElementAdapter);
                    returnAdapter = cmpSlowElementAdapter;
                }
            }
        }

        return returnAdapter;
    }

    /*
     * (non-Javadoc)
     *
     * @see org.eclipse.core.runtime.IAdapterFactory#getAdapterList()
     */
    public Class[] getAdapterList() {
        return adapterList;
    }
}
